#include "database.h"
//打开数据库
sqlite3 * db;
int open_db(){
    int rc= sqlite3_open("Barbecue.db",&db);
    if(rc!=SQLITE_OK){
        fprintf(stderr,"open_db 打开数据库失败：%s\n", sqlite3_errmsg(db));
        return -1;
    }
  //  printf("open_db打开数据库成功！\n");
    return 0;
};
//初始数据库，建表
int init_db(){
    //打开数据库
    if(open_db()!=SQLITE_OK){
        return -1;
    }
    //创建用户表
    if(create_user()!=0){
        return -1;
    }
    //创建库存表
    if(create_stock()!=0){
        return -1;
    }
    //创建会员表
    if(create_vip_user()!=0){
        return -1;
    }
//   创建管理员表
    if(create_admin_user()!=0){
        return -1;
    }
//创建员工表
    if( create_employee()!=0){
        return -1;
    }
//创建员工考勤表
    if( create_employee_record()!=0){
        return -1;
    }
    //创建菜品分类表
    if(create_foods_category()!=0){
        return -1;
    }
    //创建菜品信息表
    if(create_foods_message()!=0){
        return -1;
    }
//    创建订单表
    if(create_order()!=0){
        return -1;
    }
 //   printf("init_db:数据库初始化完成!\n");
    return 0;
}

//创建用户表
int create_user(){
      const char *sql=
              " create table if not EXISTS create_user("
              " user_id integer PRIMARY KEY AUTOINCREMENT,"
              " name text NOT NULL,"
              " password text,"
              " age integer,"
              " sex text,"
              " phone text,"
              " address text"
              " );";
      char *errmsg=NULL;
      int rc= sqlite3_exec(db,sql,NULL,NULL,&errmsg);
      if(rc!=SQLITE_OK){
          fprintf(stderr,"create_user 创建用户表失败：%s\n", errmsg);
          sqlite3_free(errmsg);
          return -1;
      }
  //  printf("create_user 用户表创建成功！\n");
      return 0;
}
//创建库存表
int create_stock(){
        const char *sql=
                     " create table if not EXISTS create_stock("
                     " stock_id integer PRIMARY key AUTOINCREMENT,"
                     " food_id integer,"
                     " food_name text,"
                     " current_quantity integer"
                     " );";
    char *errmsg=NULL;
    int rc= sqlite3_exec(db,sql,NULL,NULL,&errmsg);
    if(rc!=SQLITE_OK){
        fprintf(stderr,"create_stock 创建库存表失败：%s\n", errmsg);
        sqlite3_free(errmsg);
        return -1;
    }
   // printf("create_stock 库存表创建成功！");
    return 0;
}
//创建会员表
int create_vip_user(){
      const char *sql=
              " create table if not EXISTS create_vip_user("
              " user_vip_id integer PRIMARY KEY AUTOINCREMENT,"
              " account_id integer,"
              " password text,"
              " name text NOT NULL,"
              " age integer,"
              " sex text,"
              " phone text,"
              " vip_level integer, "
              " status tinyint NOT NULL DEFAULT 1, "
              " address text"
              " );";
    char *errmsg=NULL;
    int rc= sqlite3_exec(db,sql,NULL,NULL,&errmsg);
    if(rc!=SQLITE_OK){
        fprintf(stderr,"create_vip_user 创建会员表失败：%s\n", sqlite3_errmsg(db));
        sqlite3_free(errmsg);
        return -1;
    }
   // printf("create_vip_user 会员表创建成功！\n");
    return 0;
}
//创建管理员表
int create_admin_user(){
    const char * create_admin_user=
            " CREATE TABLE if NOT EXISTS admin("
            " id integer PRIMARY KEY AUTOINCREMENT,"
            " name text NOT NULL,"
            " phone text,"
            " password text"
            " );";
    char *errmsg=NULL;
    int rc= sqlite3_exec(db,create_admin_user,NULL,NULL,&errmsg);
    if(rc!=SQLITE_OK){
        fprintf(stderr,"create_admin_user 创建管理表失败：%s\n", errmsg);
        sqlite3_free(errmsg);
        return -1;
    }
   // printf("admin管理员表创建成功！\n");
    return 0;
}

//创建考勤表
int create_employee_record(){
    const char * create_employee_record =
            " CREATE TABLE IF NOT EXISTS attendance ("
            " attendance_id INTEGER  PRIMARY KEY AUTOINCREMENT, "
            " employee_id  INTEGER   NOT NULL, "
            " attendance_date DATE NOT NULL,"
            " clock_in_time TIME,"
            " clock_out_time TIME,"
            " attendance_status TEXT NOT NULL DEFAULT '正常',"
            " leave_hours  REAL  DEFAULT 0);";
    char *errmsg=NULL;
    int rc= sqlite3_exec(db,create_employee_record,NULL,NULL,&errmsg);
    if(rc!=SQLITE_OK){
        fprintf(stderr,"create_employee_record创建考勤表失败：%s\n", errmsg);
        sqlite3_free(errmsg);
        return -1;
    }
 //   printf("attendance考勤表创建成功！\n");
    return 0;
}
//创建员工表
int create_employee(){
    const char * create_employee=
            " CREATE TABLE if NOT EXISTS employee("
            " employee_id integer PRIMARY KEY AUTOINCREMENT,"
            " employee_name text NOT NULL,"
            " position text,"
            " employee_num text"
            " );";
    char *errmsg=NULL;
    int rc= sqlite3_exec(db,create_employee,NULL,NULL,&errmsg);
    if(rc!=SQLITE_OK){
        fprintf(stderr,"create_employee 创建员工失败：%s\n", errmsg);
        sqlite3_free(errmsg);
        return -1;
    }
  //  printf("employee员工表创建成功！\n");
    return 0;
}
//创建菜品分类表

int create_foods_category(){
    const char * create_foods_table=
            "create table if not exists foods_category"
            "("
            " category_id integer primary key,"
            " category_name text"
            " );";
    char * errmsg=NULL;
    int rc= sqlite3_exec(db,create_foods_table,NULL,NULL,&errmsg);
    if(rc!=SQLITE_OK){
        fprintf(stderr,"foods_category建表失败了:%s-----%s\n",errmsg, sqlite3_errmsg(db));
        sqlite3_free(errmsg);
        return -1;
    }
   // printf("foods_category建表成功！\n");
    return 0;
}
//创建菜品信息表
int create_foods_message(){
    const char * create_food_message_table=
            "create table if not exists foods_message"
            "("
            "foods_id integer PRIMARY KEY,"
            "foods_name text not null,"
            "price real,"
            "category_id integer"
            ");";
    char * errmsg=NULL;
    int rc= sqlite3_exec(db,create_food_message_table,NULL,NULL,&errmsg);
    if(rc!=SQLITE_OK){
        fprintf(stderr,"foods_message建表失败了:%s\n",errmsg);
        sqlite3_free(errmsg);
        return -1;
    }
   // printf("foods_message建表成功！\n");
    return 0;
}
//创建订单表
int create_order(){
    const char * create_order_table=
            "create table if not exists order_food("
            "order_id integer PRIMARY KEY,"
            "user_id integer,"
            "foods_id integer,"
            "order_time text,"
            "status text,"
            "quety integer"
            ");";
    char * errmsg=NULL;
    int rc= sqlite3_exec(db,create_order_table,NULL,NULL,&errmsg);
    if(rc!=SQLITE_OK){
        fprintf(stderr,"order建表失败了:%s-----%s\n",errmsg, sqlite3_errmsg(db));
        sqlite3_free(errmsg);
        return -1;
    }
  //  printf("order建表成功！\n");
    return 0;
}
//关闭数据库
int close_db(){
    if(db!=NULL){
        sqlite3_close(db);
        db=NULL;
    }
  //  printf("close_db:关闭数据库成功!\n");
    return 0;
};
